#include "core/itemcontroller.h"
#include "core/itemmodel.h"
#include "core/suppliercontroller.h"
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>



ItemController::ItemController(QObject *parent) :
    BaseController(parent)
{
}

ItemModel *ItemController::getItemById(int id)
{
    QSqlQuery query;
    QString sql;
    ItemModel *item = NULL;
    SupplierModel *supplier = NULL;

    sql = "SELECT i.name, i.code, i.supplierID, i.srp, i.active, "
            "s.name, s.contactNumber, s.country "
            "FROM Items AS i LEFT OUTER JOIN Suppliers AS s "
            "ON i.supplierID = s.ID "
            "WHERE i.ID = :id";
    query.prepare(sql);
    query.bindValue(":id", id);

    if (!query.exec()) {
        qDebug() << "ItemController::getItemById query failed!"
                 << query.lastError().text();
    }

    // If there is no item with the specified ID, this will return false,
    // and we'll return NULL.
    if (query.next()) {
        item = new ItemModel;
        supplier = new SupplierModel;

        supplier->setID(query.value(2).toInt());
        supplier->setName(query.value(5).toString());
        supplier->setContactNumber(query.value(6).toString());
        supplier->setCountry(query.value(7).toString());

        item->setID(id);
        item->setName(query.value(0).toString());
        item->setCode(query.value(1).toString());
        item->setSupplier(supplier);
        item->setSrp(query.value(3).toFloat());
        item->setActive(query.value(4).toBool());
    }

    return item;
}

void ItemController::addItem(ItemModel *item) {
    QString sql;
    QSqlQuery query;

    sql = "INSERT INTO Items "
            "(name, code, supplierID, srp, active) "
            "VALUES (:name, :code, :supplierID, :srp, :active) ";

    query.prepare(sql);
    query.bindValue(":name", item->getName());
    query.bindValue(":code", item->getCode());
    query.bindValue(":supplierID", item->getSupplier()->getID());
    query.bindValue(":srp", item->getSrp());
    query.bindValue(":active", item->isActive());

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "ItemController::addItem Query failed!" << query.lastError().text();
    }
}

void ItemController::updateItem(ItemModel *item) {
    QString sql;
    QSqlQuery query;

    sql = "UPDATE Items "
            "SET name = :name, "
            "code = :code, "
            "supplierID = :supplierID, "
            "srp = :srp, "
            "active = :active "
            "WHERE ID = :ID";

    query.prepare(sql);
    query.bindValue(":name", item->getName());
    query.bindValue(":code", item->getCode());
    query.bindValue(":supplierID", item->getSupplier()->getID());
    query.bindValue(":srp", item->getSrp());
    query.bindValue(":active", item->isActive());
    query.bindValue(":ID", item->getID());

    bool ok = query.exec();

    if(!ok) {
        qDebug() << "ItemController::updateItem Query failed!" << query.lastError().text();
        qDebug() << "ID is: " << item->getID();
    }

}

QList<BaseModel *> ItemController::getAll()
{
    QList<BaseModel *> list;
    ItemModel *item;
    SupplierModel *supplier;
    QSqlQuery query;
    SupplierController ctrl;
    bool ok;

    QString sql = "SELECT i.ID, i.name, i.code, i.supplierID, i.srp, i.active, "
            "s.ID, s.name, s.contactNumber, s.country, "
            "(SELECT COUNT(*) FROM Stocks AS s WHERE s.itemID = i.ID) "
            "FROM Items AS i LEFT OUTER JOIN Suppliers AS s "
            "ON i.supplierID = s.ID";

    ok = query.exec(sql);

    if(!ok) {
        /* TODO: Print detailed error here */
        qDebug() << "ItemController: Query Failed! NO!";
    }

    while (query.next()) {
        item = new ItemModel();
        supplier = new SupplierModel();
        supplier->setID(query.value(6).toInt());
        supplier->setName(query.value(7).toString());
        supplier->setContactNumber(query.value(8).toString());
        supplier->setCountry(query.value(9).toString());

        item->setID(query.value(0).toInt());
        item->setName(query.value(1).toString());
        item->setCode(query.value(2).toString());
        item->setSupplier(supplier);
        item->setSrp(query.value(4).toFloat());
        item->setActive(query.value(5).toBool());
        item->setNumStocks(query.value(10).toInt());

        list.append(item);
    }

    emit update(list);
    return list;
}

QList<BaseModel *> ItemController::getBestsellersForMonth(QDate month)
{
    return getAll();
}

QList<BaseModel *> ItemController::getAllDwindling()
{
    QList <BaseModel *> itemCopyList;
    QList <BaseModel *> list;
    ItemModel *item;

    itemCopyList = this->getAll();
    int i;

    for( i = 0 ; i < itemCopyList.length() ; i++ ) {
        item = (ItemModel *) itemCopyList.at(i);
        if ( item->getNumStocks() < 5 ) {
            list.append(item);
        }
    }
    return list;
}
